package xin.clips.web.controller.gy;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import xin.clips.common.annotation.Log;
import xin.clips.common.annotation.VisistLog;
import xin.clips.common.enums.BusinessType;
import xin.clips.common.core.controller.BaseController;
import xin.clips.common.core.domain.AjaxResult;
import xin.clips.common.enums.gy.DeviceTypeEnum;
import xin.clips.common.utils.poi.ExcelUtil;
import xin.clips.common.core.page.TableDataInfo;
import xin.clips.system.domain.gy.*;
import xin.clips.system.service.gy.IGyArmParamRecordService;
import xin.clips.system.service.gy.IGyDeviceAlarmRecordService;
import xin.clips.system.service.gy.IGyMachineParamRecordService;

/**
 * 设备预警记录Controller
 *
 * @author clips
 * @date 2023-03-15
 */
@Controller
@RequestMapping("/gy/alarmRecord")
public class GyDeviceAlarmRecordController extends BaseController
{
    private String prefix = "gy/alarmRecord";

    @Autowired
    private IGyDeviceAlarmRecordService gyDeviceAlarmRecordService;

    @Autowired
    private IGyArmParamRecordService armParamRecordService;

    @Autowired
    private IGyMachineParamRecordService machineParamRecordService;

    @RequiresPermissions("gy:alarmRecord:view")
    @VisistLog(title = "设备预警记录")
    @GetMapping()
    public String alarmRecord()
    {
        return prefix + "/alarmRecord";
    }

    /**
     * 查询设备预警记录列表
     */
    @RequiresPermissions("gy:alarmRecord:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GyDeviceAlarmRecord gyDeviceAlarmRecord)
    {
        startPage();
        List<GyDeviceAlarmRecord> list = gyDeviceAlarmRecordService.selectGyDeviceAlarmRecordList(gyDeviceAlarmRecord);
        return getDataTable(list);
    }

    /**
     * 导出设备预警记录列表
     */
    @RequiresPermissions("gy:alarmRecord:export")
    @Log(title = "设备预警记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GyDeviceAlarmRecord gyDeviceAlarmRecord)
    {
        List<GyDeviceAlarmRecord> list = gyDeviceAlarmRecordService.selectGyDeviceAlarmRecordList(gyDeviceAlarmRecord);
        ExcelUtil<GyDeviceAlarmRecord> util = new ExcelUtil<GyDeviceAlarmRecord>(GyDeviceAlarmRecord.class);
        return util.exportExcel(list, "设备预警记录数据");
    }

    /**
     * 新增设备预警记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备预警记录
     */
    @RequiresPermissions("gy:alarmRecord:add")
    @Log(title = "设备预警记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GyDeviceAlarmRecord gyDeviceAlarmRecord)
    {
        return toAjax(gyDeviceAlarmRecordService.insertGyDeviceAlarmRecord(gyDeviceAlarmRecord));
    }

    /**
     * 修改设备预警记录
     */
    @RequiresPermissions("gy:alarmRecord:edit")
    @GetMapping("/edit/{alarmId}")
    public String edit(@PathVariable("alarmId") Long alarmId, ModelMap mmap)
    {
        GyDeviceAlarmRecord gyDeviceAlarmRecord = gyDeviceAlarmRecordService.selectGyDeviceAlarmRecordByAlarmId(alarmId);
        mmap.put("gyDeviceAlarmRecord", gyDeviceAlarmRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备预警记录
     */
    @RequiresPermissions("gy:alarmRecord:edit")
    @Log(title = "设备预警记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GyDeviceAlarmRecord gyDeviceAlarmRecord)
    {
        return toAjax(gyDeviceAlarmRecordService.updateGyDeviceAlarmRecord(gyDeviceAlarmRecord));
    }

    /**
     * 删除设备预警记录
     */
    @RequiresPermissions("gy:alarmRecord:remove")
    @Log(title = "设备预警记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gyDeviceAlarmRecordService.deleteGyDeviceAlarmRecordByAlarmIds(ids));
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    public String detail(@RequestParam("alarmId") String alarmId, ModelMap mmap) {
        GyDeviceAlarmRecord alarmRecord =
                gyDeviceAlarmRecordService.selectGyDeviceAlarmRecordByAlarmId(Long.valueOf(alarmId));
        GyMachineParamRecord machineParam = new GyMachineParamRecord();
        GyArmParamRecord armParam = new GyArmParamRecord();
        if (alarmRecord.getDeviceType().equals(DeviceTypeEnum.MACHINE.getInfo())) {
            machineParam = machineParamRecordService.
                    selectGyMachineParamRecordByRecordId(alarmRecord.getAlarmId());
        } else if (alarmRecord.getDeviceType().equals(DeviceTypeEnum.ARM.getInfo())) {
            armParam = armParamRecordService.selectGyArmParamRecordByRecordId(alarmRecord.getAlarmId());
        }
        mmap.put("record", alarmRecord);
        mmap.put("armParam", armParam);
        mmap.put("machineParam", machineParam);

        return prefix + "/detail";
    }
}
